/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.solution;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ejb.EJB;
import model.entity.Customerdemand;
import model.entity.Operationrecord;
import model.entity.Problem;
import model.entity.Solution;
import model.entity.Task;
import model.entity.TaskCustomerdemand;
import org.omnifaces.util.Faces;
import service.ITaskmanager;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Named(value = "solutionViewMB")
@SessionScoped
public class SolutionViewMB implements Serializable {

    @EJB(beanName = "ProblemManager")
    private InterfaceServiceLocal<Problem> problemManager;
    @EJB(beanName = "SolutionManager")
    private InterfaceServiceLocal<Solution> solutionManager;
    @EJB(beanName = "TaskManager")
    private ITaskmanager<Task> taskManager;
    @EJB(beanName = "OperationrecordManager")
    private InterfaceServiceLocal<Operationrecord> operationrecordManager;
    String idsolutionparam;
    String idsolution;
    Problem problem;
    Solution solution;
    Task task;
    private Operationrecord operationrecord;
    private int status;

    public void prerender() {

        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        idsolution = idsolutionparam;

        idsolutionparam = null;
        init();

    }

    public void init() {

        if (idsolution != null) {

            solution = solutionManager.find(Integer.parseInt(idsolution));
            problem = solution.getIdproblem();

            ArrayList pList = new ArrayList();
            pList.add(problem.getIdproblem());
            operationrecord = operationrecordManager.findOneItem("select o from Operationrecord o where o.idsolution in (select s from Solution s where s.idproblem.idproblem=?1 )", pList);
            if ((operationrecord == null || operationrecord.getState() == 0) && solution.getState() == 1) {
                status = 1;//可执行
            } else {
                status = 0;//不可执行
            }

            solution.setTaskList(new ArrayList(solution.getTaskList()));
            for (Task t : solution.getTaskList()) {
                ArrayList<TaskCustomerdemand> taskCustomerdemandList = new ArrayList(t.getTaskCustomerdemandList());
                t.setTaskCustomerdemandList(taskCustomerdemandList);
//                double TimeCost = tl.stream().mapToDouble(s -> s.getTimeCost()).reduce((s1, s2) -> s1 + s2).getAsDouble();
                t.setRealcap((int) taskCustomerdemandList.stream().mapToDouble(s -> s.getIdcustomerdemand().getRequirement()).reduce((s1, s2) -> s1 + s2).getAsDouble());
                //paixu
                Collections.sort(t.getTaskCustomerdemandList(), new TaskCustomerdemandComparator());;
            }

        } else {

        }

    }

    public void execute() {
        Operationrecord operationrecord = new Operationrecord();
        operationrecord.setIdsolution(solution);
        operationrecord.setFordate(solution.getIdproblem().getFordate());
        operationrecord.setState(1);
        problem.setIdsolution(solution);
        operationrecordManager.edit(operationrecord);
        problemManager.edit(problem);
        status = 0;
    }

    public class TaskCustomerdemandComparator implements Comparator<TaskCustomerdemand> {

        @Override
        public int compare(TaskCustomerdemand o1, TaskCustomerdemand o2) {
            return o1.getVisiteorder() - o2.getVisiteorder();
        }
    }

    /**
     * Creates a new instance of SolutionViewMB
     */
    public SolutionViewMB() {
    }

    public String getIdsolutionparam() {
        return idsolutionparam;
    }

    public void setIdsolutionparam(String idsolutionparam) {
        this.idsolutionparam = idsolutionparam;
    }

    public String getIdsolution() {
        return idsolution;
    }

    public void setIdsolution(String idsolution) {
        this.idsolution = idsolution;
    }

    public Problem getProblem() {
        return problem;
    }

    public void setProblem(Problem problem) {
        this.problem = problem;
    }

    public Solution getSolution() {
        return solution;
    }

    public void setSolution(Solution solution) {
        this.solution = solution;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public Operationrecord getOperationrecord() {
        return operationrecord;
    }

    public void setOperationrecord(Operationrecord operationrecord) {
        this.operationrecord = operationrecord;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

}
